#Journal: Journal of Politics

#Load in the RData file and relevant packages

load("Credit Claiming in the EU - Data for Tables and Figures.RData")
library(ggplot2)
library(cowplot)
library(TMB)
library(glmmTMB)
library(dplyr)
library(ggeffects)
library(quanteda)

#Figure 1: the Increase in public and partisan Euroscepticism
  
xbreaks1.1 <- c("2005-h1", "2005-h2", "2006-h1", "2006-h2", "2007-h1", "2007-h2", "2008-h1", "2008-h2", "2009-h1", "2009-h2", "2010-h1", "2010-h2", "2011-h1", "2011-h2", "2012-h1", "2012-h2", "2013-h1", "2013-h2", "2014-h1", "2014-h2", "2015-h1", "2015-h2", "2016-h1", "2016-h2", "2017-h1", "2017-h2", "2018-h1", "2018-h2" )
xlabels1.1 <- c("2005", "", "2006", "", "2007", "", "2008", "", "2009", "", "2010", "", "2011", "", "2012", "", "2013", "", "2014", "", "2015", "", "2016", "", "2017", "", "2018", "" )

f1.1 <- ggplot(public.euroscepticism, aes(x = Date, y = Negative.EU.image, group = 1))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  scale_x_discrete(breaks= xbreaks1.1, labels = xlabels1.1)+
  ylab("Share of negative EU image in\npublic opinion (Eurobarometer, EU-28 average)\n") + xlab("") +
  coord_cartesian(ylim = c(0.1, 0.3))+
  theme(plot.title = element_text(size=10, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 10),
        axis.text.x=element_text(angle=90, size=10, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=8, colour = "black")) + 
  labs(title = "Public Euroscepticism")


EUscepticseatshare$Year <- as.character(EUscepticseatshare$Year)
xbreaks1.2 <- c("2005", "2006", "2007", "2008", "2009","2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017","2018")
xlabels1.2 <- c("2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017","2018")

f1.2 <- ggplot(EUscepticseatshare, aes(x = Year, y = Share, group = 1))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  scale_x_discrete(breaks= xbreaks1.2, labels = xlabels1.2)+
  ylab("Seat share of Eurosceptic parties in\n National Parliaments (EU-28 average)\n") + xlab("") + 
  coord_cartesian(ylim = c(0.05, 0.2))+
  theme(plot.title = element_text(size=10, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 10),
        axis.text.x=element_text(angle=90, size=10, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=8, colour = "black"))+
  labs(title = "Partisan Euroscepticism")

Figure1 <- plot_grid(f1.1, f1.2) + labs(title = "") + theme(plot.title = element_text(size=12))
print(Figure1)

#Figure 2: The Increase in Credit Claiming in National Governments' EU Rhetoric

xbreaks <- c("2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017","2018")
xlabels <- c("2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017","2018")

ggplot(proportions.all.leaders, aes(x = year, y = proportion, group = group, col=group, fill=group))+
  geom_point(size = 1)+
  scale_colour_manual(values = c ('#800000', '#000000', '#000000'))+
  scale_fill_manual(values = c ('#800000', '#D3D3D3', '#696969'))+
  geom_smooth(span = 0.9, size = 0.5)+
  scale_x_discrete(breaks= xbreaks, labels = xlabels)+
  ylab("Prop. of statements\n(loess smoother, span = 0.2, 95% c.i.)\n") + xlab("") +
  coord_cartesian(ylim = c(0, 0.375))+
  theme(plot.title = element_text(size=12),
        plot.subtitle = element_text(size=9),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 10),
        axis.text.x=element_text(angle=90, size=10, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=10, colour = "black")) +
  labs(caption = "Source: 5,943 handcoded statements\n(excludes statements from interim prime ministers)", 
       x = "", y = "Prop. of statements\n(loess smoother, span = 0.2, 95% c.i.)\n") 



#Figure 3: Balance of Credit Sharing and Credit Claiming in National Incumbents' EU rhetoric

leaderbalance %>%
  arrange(balance) %>%
  mutate(Head.of.Government=factor(Head.of.Government, Head.of.Government)) %>%
  
  ggplot(aes(x=Head.of.Government, y=balance,)) + 
  geom_point(stat='identity', fill="black", size=2)  +
  geom_hline(yintercept = 0, linetype = "dashed") + 
  ylim(-1, 1) +
  coord_flip() +
  labs(
    caption = "Source: 5,943 handcoded statements", 
    x = "", y = "Prop. Credit Share - Prop. Credit Claim") +
  theme(plot.title = element_text(size=11),
        plot.subtitle = element_text(size=9))


#Figure 4: The balance of credit sharing and credit claiming differs for high salience and low salience issues


p1 <-leaderbalance.economic.situation %>%
  arrange(balance) %>%
  mutate(Head.of.Government=factor(Head.of.Government, Head.of.Government)) %>%
  
  ggplot(aes(x=Head.of.Government, y=balance,)) + 
  geom_point(stat='identity', fill="black", size=2)  +
  geom_hline(yintercept = 0, linetype = "dashed") + 
  ylim(-1, 1) +
  coord_flip() +
  labs(title = "4.1 EB Policy Issue: Economic Situ",
       caption = "Source: 2,157 statements", 
       x = "", y = "Prop. Share - Prop. Claim") +
  theme(plot.title = element_text(size=10.5),
        plot.subtitle = element_text(size=9))


p2 <-leaderbalance.environment %>%
  arrange(balance) %>%
  mutate(Head.of.Government=factor(Head.of.Government, Head.of.Government)) %>%
  
  ggplot(aes(x=Head.of.Government, y=balance,)) + 
  geom_point(stat='identity', fill="black", size=2)  +
  geom_hline(yintercept = 0, linetype = "dashed") + 
  ylim(-1, 1) +
  coord_flip() +
  labs(title = "4.2 EB Policy Issue: Environment",
       caption = "Source: 351 statements", 
       x = "", y = "Prop. Share - Prop. Claim") +
  theme(plot.title = element_text(size=10.5),
        plot.subtitle = element_text(size=9))


plot_grid(p1, p2)  + theme(plot.title = element_text(size=11),
                           plot.subtitle = element_text(size=9),
                           plot.caption = element_text(size=8))


#Figure 5: Marginal effects of issue salience on credit claiming and credit sharing

table3.3 <- glm(Claim ~ Partisan.Euroscepticism + Public.Euroscepticism + Issue.Salience + Gov.Trust + Gov.EU.position + Gov.EU.division + EU.presidency + unemployment + election.year + Country, data = EUCO, family = "binomial")
table3.4 <- glm(Share ~ Partisan.Euroscepticism + Public.Euroscepticism + Issue.Salience + Gov.Trust + Gov.EU.position + Gov.EU.division + EU.presidency + unemployment + election.year + Country, data = EUCO, family = "binomial")

p1 <- ggpredict(table3.3, "Issue.Salience")
p1.0 <- ggplot(p1, aes(x, predicted)) + geom_line() + geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .1) + labs(title = "5.1 Credit Claiming\n", 
                                                                                                                             x = "\nIssue Salience", y = "Predicted Probability\n") + theme(plot.title = element_text(size=11)  )

p2 <- ggpredict(table3.4, "Issue.Salience")
p2.0 <- ggplot(p2, aes(x, predicted)) + geom_line() + geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .1) + labs(title = "5.2 Credit Sharing\n", 
                                                                                                                             x = "\nIssue Salience", y = "Predicted Probability\n") + theme(plot.title = element_text(size=11)  )

plot_grid(p1.0, p2.0) + theme(plot.title = element_text(size=12))


#Figure A1.1: Length of Speeches in EUCOSpeech

#set up a multi-panel layout
par(mfrow=c(4, 2), mar=c(4, 4, 2, 1), oma=c(0, 0, 2, 0))

#histograms


de <- subset(EUCO, EUCO$Country == "DE")
de$tokens <- ntoken(de$Text)
de.agg.tokens <- aggregate(de$tokens,by = list(de$SpeechID), FUN=sum)
names(de.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(de.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'GERMANY',  xlim = c(0, 5000))
abline(v = mean(de.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

dn <- subset(EUCO, EUCO$Country == "DN")
dn$tokens <- ntoken(dn$Text)
dn.agg.tokens <- aggregate(dn$tokens,by = list(dn$SpeechID), FUN=sum)
names(dn.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(dn.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'DENMARK',  xlim = c(0, 5000))
abline(v = mean(dn.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

es <- subset(EUCO, EUCO$Country == "ES")
es$tokens <- ntoken(es$Text)
es.agg.tokens <- aggregate(es$tokens,by = list(es$SpeechID), FUN=sum)
names(es.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(es.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'SPAIN',  xlim = c(0, 5000))
abline(v = mean(es.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

fr <- subset(EUCO, EUCO$Country == "FR")
fr$tokens <- ntoken(fr$Text)
fr.agg.tokens <- aggregate(fr$tokens,by = list(fr$SpeechID), FUN=sum)
names(fr.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(fr.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'FRANCE', xlim = c(0, 5000))
abline(v = mean(fr.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

gr <- subset(EUCO, EUCO$Country == "GR")
gr$tokens <- ntoken(gr$Text)
gr.agg.tokens <- aggregate(gr$tokens,by = list(gr$SpeechID), FUN=sum)
names(gr.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(gr.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'GREECE',  xlim = c(0, 5000))
abline(v = mean(gr.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

irl <- subset(EUCO, EUCO$Country == "IRL")
irl$tokens <- ntoken(irl$Text)
irl.agg.tokens <- aggregate(irl$tokens,by = list(irl$SpeechID), FUN=sum)
names(irl.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(irl.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'IRELAND',  xlim = c(0, 5000))
abline(v = mean(irl.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

uk <- subset(EUCO, EUCO$Country == "UK")
uk$tokens <- ntoken(uk$Text)
uk.agg.tokens <- aggregate(uk$tokens,by = list(uk$SpeechID), FUN=sum)
names(uk.agg.tokens) <- c("speech", "speech.length.in.tokens")
hist(uk.agg.tokens$speech.length.in.tokens, xlab = 'Tokens in Speech',main = 'UNITED KINGDOM', xlim = c(0, 5000))
abline(v = mean(uk.agg.tokens$speech.length.in.tokens), col = "red", lwd = 3)

# Reset the plotting parameters
par(mfrow=c(1, 1), oma=c(0, 0, 0, 0))

#Figure A2: Credit Claiming, Credit Sharing, and Blame Shifting in the Rhetoric of CEE incumbents

cee.countries$count <- 1
cee.countries$Share <- as.character(cee.countries$Share)
cee.countries$Claim <- as.character(cee.countries$Claim)
cee.countries$Blame <- as.character(cee.countries$Blame)
cee.countries$Share <- as.numeric(cee.countries$Share)
cee.countries$Claim <- as.numeric(cee.countries$Claim)
cee.countries$Blame <- as.numeric(cee.countries$Blame)

aggcount2 <- aggregate(cbind(cee.countries$Share, cee.countries$Claim, cee.countries$Blame, cee.countries$count), by = list(cee.countries$Year), FUN=sum)
names(aggcount2) <- c("year", "share", "claim", "blame", "counts")
aggcount2$propshare <- aggcount2$share / aggcount2$counts
aggcount2$propclaim <- aggcount2$claim / aggcount2$counts
aggcount2$blameshare <- aggcount2$blame / aggcount2$counts

claim <-aggcount2[, c(1,7)]
claim$group <- "credit claiming"
names(claim) <- c("year", "proportion", "group")

share <-aggcount2[, c(1,6)]
share$group <- "credit sharing"
names(share) <- c("year", "proportion", "group")

blame <-aggcount2[, c(1,8)]
blame$group <- "blame shifting"
names(blame) <- c("year", "proportion", "group")

proportions <- rbind(claim, share, blame)
proportions$group <- as.character(proportions$group)
proportions <- subset(proportions, proportions$year >2010)
proportions$year <- as.character(proportions$year)


xbreaks <- c("2011", "2012", "2013", "2014", "2015", "2016", "2017","2018")
xlabels <- c( "2011", "2012", "2013", "2014", "2015", "2016", "2017","2018")

ggplot(proportions, aes(x = year, y = proportion, group = group, col=group, fill=group))+
  geom_point(size = 1)+
  scale_colour_manual(values = c ('#800000', 'darkblue', 'deepskyblue2'))+
  geom_line(size = 0.5)+
  scale_x_discrete(breaks= xbreaks, labels = xlabels)+
  ylab("Prop. of statements\n(loess smoother, span = 0.2, 95% c.i.)\n") + xlab("") +
  coord_cartesian(ylim = c(0, 0.5))+
  theme(plot.title = element_text(size=12),
        plot.subtitle = element_text(size=9),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 10),
        axis.text.x=element_text(angle=90, size=10, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=10, colour = "black")) +
  labs(caption = "Source: 553 statements from incubents in Hungary and Slovakia (Orban, Fico, Radicova, Pellegrini)", 
       x = "", y = "Prop. of statements\n") 


#Figure A3: Rhetoric of CEE incumbents - Breakdown by leader

cee.countries$count <- 1
cee.countries$Share <- as.character(cee.countries$Share)
cee.countries$Claim <- as.character(cee.countries$Claim)
cee.countries$Blame <- as.character(cee.countries$Blame)
cee.countries$Share <- as.numeric(cee.countries$Share)
cee.countries$Claim <- as.numeric(cee.countries$Claim)
cee.countries$Blame <- as.numeric(cee.countries$Blame)

orban <- subset(cee.countries, cee.countries$Head.of.Government == "Orban")
orban$counts <- 1
orban.agg <- aggregate(cbind(orban$counts), by = list(orban$Style), FUN=sum)
names(orban.agg) <- c("Style", "statements")
orban.agg$share <- orban.agg$statements/245
orban.agg <- subset(orban.agg, orban.agg$Style != "Descriptive")
orban.agg$Style = factor(orban.agg$Style , levels = c("Credit Claiming", "Credit Sharing", "Blame Shifting"))
orban.agg$leader <- "orban"
g1 <- ggplot(orban.agg, aes(fill=Style, y=share, x= leader)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("darkblue", "deepskyblue2", "red3"))  + labs(title="HU - Orban (n=245)",
                                                                                                                          x ="", y = "Share of Statements") + theme(text=element_text(size=10)) + theme(legend.position = "none", axis.text.x =element_blank()) + ylim(0,0.61)
g1.with.legend <- ggplot(orban.agg, aes(fill=Style, y=share, x= leader)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("darkblue", "deepskyblue2", "red3"))  + labs(title="HU - Orban (n=245)",
                                                                                                                          x ="", y = "Share of Statements") + theme(text=element_text(size=10))  + theme(legend.title = element_blank()) + ylim(0,0.61)


fico <- subset(cee.countries, cee.countries$Head.of.Government == "Fico")
fico$counts <- 1
fico.agg <- aggregate(cbind(fico$counts), by = list(fico$Style), FUN=sum)
names(fico.agg) <- c("Style", "statements")
fico.agg$share <- fico.agg$statements/265
fico.agg <- subset(fico.agg, fico.agg$Style != "Descriptive")
fico.agg$Style = factor(fico.agg$Style , levels = c("Credit Claiming", "Credit Sharing", "Blame Shifting"))
fico.agg$leader <- "Fico"
g2 <- ggplot(fico.agg, aes(fill=Style, y=share, x= leader)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("darkblue", "deepskyblue2", "red3"))  + labs(title="SK - Fico (n=265)",
                                                                                                                          x ="", y = "") + theme(text=element_text(size=10)) + theme(legend.position = "none", axis.text.x =element_blank()) +  ylim(0,0.61)

pellegrini <- subset(cee.countries, cee.countries$Head.of.Government == "Pellegrini")
pellegrini$counts <- 1
pellegrini.agg <- aggregate(cbind(pellegrini$counts), by = list(pellegrini$Style), FUN=sum)
names(pellegrini.agg) <- c("Style", "statements")
pellegrini.agg$share <- pellegrini.agg$statements/29
pellegrini.agg <- subset(pellegrini.agg, pellegrini.agg$Style != "Descriptive")
pellegrini.agg$Style = factor(pellegrini.agg$Style , levels = c("Credit Claiming", "Credit Sharing", "Blame Shifting"))
pellegrini.agg$leader <- "pellegrini"
g3 <- ggplot(pellegrini.agg, aes(fill=Style, y=share, x= leader)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("darkblue", "deepskyblue2", "red3"))  + labs(title="SK - Pellegrini (n=30)",
                                                                                                                          x ="", y = "") + theme(text=element_text(size=10)) + theme(legend.position = "none", axis.text.x =element_blank())  + ylim(0,0.61)


radicova <- subset(cee.countries, cee.countries$Head.of.Government == "Radicova")
radicova$counts <- 1
radicova.agg <- aggregate(cbind(radicova$counts), by = list(radicova$Style), FUN=sum)
names(radicova.agg) <- c("Style", "statements")
radicova.agg$share <- radicova.agg$statements/13
radicova.agg <- subset(radicova.agg, radicova.agg$Style != "Descriptive")
radicova.agg$Style = factor(radicova.agg$Style , levels = c("Credit Claiming", "Credit Sharing", "Blame Shifting"))
radicova.agg$leader <- "radicova"
g4 <- ggplot(radicova.agg, aes(fill=Style, y=share, x= leader)) + 
  geom_bar(position="stack", stat="identity") + scale_fill_manual(values = c("darkblue", "deepskyblue2", "red3"))  + labs(title="SK - Radicova (n=13)",
                                                                                                                          x ="", y = "") + theme(text=element_text(size=10)) + theme(legend.position = "none", axis.text.x =element_blank()) + ylim(0,0.61)

cee <- plot_grid(g1, g2, g3, g4, ncol=4)
legend_g1 <- get_legend(g1.with.legend) 
plot_grid(g1, g2, g3, g4, legend_g1, ncol = 5, rel_widths = c(1, 1, 1, 1, 0.5))

#Figure A4: British PMs Use Less Negative Language when Talking About the EU 

uk.pms <- cbind(uk.pms.agg.eu, uk.pms.agg.total)
uk.pms$negshare.eu.clean <- uk.pms$eu.neg.clean / uk.pms$eu.tokens.clean
uk.pms$negshare.total.clean <- uk.pms$total.neg.clean / uk.pms$total.tokens.clean
uk.pms$diff <- uk.pms$negshare.eu.clean - uk.pms$negshare.total.clean
uk.pms <- subset(uk.pms, select = -c(8))
uk.pms$speaker <- factor(uk.pms$speaker, levels = c("Tony Blair", "Theresa May", "Gordon Brown", "John Major", "David Cameron"))


ggplot(uk.pms, aes(x=speaker, y=diff)) + 
  geom_point(size = 2) +
  geom_hline(yintercept = 0, linetype = "dashed") + 
  coord_flip() +
  labs(caption = "Source: 42,481 speeches and 10,406 statements on the EU by PMs in the House of Commons (1991-2018)", 
       x = "", y = "\nNeg. Share EU Comms - Neg. Share Total Comms\n") +
  theme(plot.title = element_text(size=11),
        plot.subtitle = element_text(size=9))
